<template>
  <div>
    <div class="table">
      <el-table :data="tableData" stripe >
        <el-table-column type="index" :index="hIndex" label="序号" width="70"></el-table-column>
        <el-table-column prop="cause" label="报销原因" show-overflow-tooltip></el-table-column>
        <el-table-column prop="createTime" label="申请时间"></el-table-column>
        <el-table-column prop="expensesType" label="报销类型"></el-table-column>
        <el-table-column prop="status" label="报销状态"></el-table-column>
        <el-table-column prop="totalAmount" label="报销金额"></el-table-column>
        <el-table-column label="查看" width="180" align="center">
          <template v-slot="scope">
            <el-button plain type="primary" @click="getExpensesInfo(scope.row.id)" size="mini">查看</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template v-slot="scope">
            <el-button plain type="primary" @click="confirmLoan(scope.row)" size="mini">确认打款</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
            background
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[5, 10, 20]"
            :page-size="pageSize"
            layout="total, prev, pager, next"
            :total="total">
        </el-pagination>
      </div>
    </div>

  </div>
</template>

<script>
export default {
  name: "Loan",
  data() {
    return {
      tableData: [],  // 所有的数据
      pageNum: 1,   // 当前的页码
      pageSize: 10,  // 每页显示的个数
      total: 0,
      user: JSON.parse(localStorage.getItem('xm-user') || '{}'),
    }
  },
  created() {
    console.log(this.user)
    this.load(1)
  },
  methods: {
    //确认放款操作
    confirmLoan(row){
      this.$confirm('您确定打款吗？', '确认', {type: "warning"}).then(response => {
        this.$request.put('/loan/updateStatus/' + row.id).then(res => {
          if (res.code === '200') {   // 表示操作成功
            this.$message.success('操作成功')
            this.load(1)
          } else {
            this.$message.error(res.msg)  // 弹出错误的信息
          }
        })
      }).catch(() => {
      })
    },
    //查看审批单的详细信息
    getExpensesInfo(id) {
      this.$router.push('/expensesInfo?expensesId='+id)
    },
    save() {   // 保存按钮触发的逻辑  它会触发新增或者更新
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.$request({
            url: this.form.id ? '/loan/update' : '/loan/add',
            method: this.form.id ? 'PUT' : 'POST',
            data: this.form
          }).then(res => {
            if (res.code === '200') {  // 表示成功保存
              this.$message.success('保存成功')
              this.load(1)
              this.fromVisible = false
            } else {
              this.$message.error(res.msg)  // 弹出错误的信息
            }
          })
        }
      })
    },
    load(pageNum) {  // 分页查询
      if (pageNum) this.pageNum = pageNum
      this.$request.get('/loan/selectPage', {
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          title: this.title,
        }
      }).then(res => {
        this.tableData = res.data?.list
        this.total = res.data?.total
      })
    },
    handleCurrentChange(pageNum) {
      this.load(pageNum)
    },
    // 当前页数 - 1 * 每页数据条数 + index + 1 （ index 是索引值，从0开始）
    hIndex(index) {
      return (this.pageNum - 1) * this.pageSize + index + 1;
    }
  },
}
</script>

<style scoped>

</style>
